import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    rowCount: 0,
    floatCount: 23, // 微信悬浮框显示数字
    selectedItemsPerPage: {}, // 存储每个页面的已选择商品
    XAccessToken: '', //X-Access-Token
    SysAccessToken: 'YoLEiEsVOa/jE4p9gDqo41DwCZl58lz8b2GNAR9b6kw=', // Sys-Access-Token
    redirect: '', //存储登录后重定向地址
    tenantId: '', //租户id
    shopid: '' //店铺id
  },
  getters: {
    rowCount: state => state.rowCount,
    floatCount: state => state.floatCount 
  },
  mutations: {
    setRowCount(state, count) {
      state.rowCount = count;
    },
    setFloatCount(state,count){
      state.floatCount = count;
    },
    setSelectedItems(state, { pageName, items }) {
      Vue.set(state.selectedItemsPerPage, pageName, items);
    },
    clearSelectedItems(state, pageName) {
      Vue.set(state.selectedItemsPerPage, pageName, []);
    },

  },
  actions: {
    updateRowCount({ commit }, count) {
      commit('setRowCount', count);
    },
    upFloatCount({commit},count){
      commit('setFloatCount',count);
    },
    setSelectedItems({ commit }, { pageName, items }) {
      commit('setSelectedItems', { pageName, items });
    },
    clearSelectedItems({ commit }, pageName) {
      commit('clearSelectedItems', pageName);
    },
  },
  modules: {
    addCommodity: {
      state: {
        //BasicInformation.vue
        ruleForm: {
          commodityName: '',
          commodityType: [],
          brand: '',
          productSn: '',
          commodityUnit: '',
          commodityIntro: '',
          imageInfos:[],
          pic: '',
        },

        //CommodityAttributes.vue
        specForm: {
          bodyColors: [],
          storageCapacities: [],
          networkTypes: [],
          screenSizes: '',
          systems: '',
          batteryCapacities: '',
          sku: '',
          firstAttributeSaveDTOS: [],
          skuStockList: [],
        }
      },
      mutations: {
        // 更新 ruleForm 数据
        updateRuleFormData(state, payload) {
          console.log('RuleForm update:', state.ruleForm);
          Object.assign(state.ruleForm, payload);
        },
    
        // 更新 specForm 数据
        updateSpecFormData(state, payload) {
          console.log('SpecFor update:', state.specForm);
          Object.assign(state.specForm, payload);
        },
      },
      actions: {
        // 更新 ruleForm 数据
        updateRuleFormData({ commit }, payload) {
          commit('updateRuleFormData', payload);
        },
    
        // 更新 specForm 数据
        updateSpecFormData({ commit }, payload) {
          commit('updateSpecFormData', payload);
        }
      },
      getters: {
        // 创建一个 getter 来获取 commodityType 的最后一个元素
        lastCommodityTypeItem: state => {
          // console.log('state.ruleForm.commodityType:', state.ruleForm.commodityType);
          return state.addCommodity.ruleForm.commodityType[state.addCommodity.ruleForm.commodityType.length - 1];
        }
      },
    }
  }
})
